﻿using AppCommerciaux.Entity;
using AppCommerciaux.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AppCommerciaux.ViewModels
{
    public class CommandeEditViewModel : ViewModelBase
    {
        #region Constructeur

        public CommandeEditViewModel(string etat, commandeclient selectedCommande)
        {
            this.Etat = etat;
            this.SelectedCommande = selectedCommande;
            this.SelectedCommande.tiers_contact = new tiers_contact();
            this.SelectedCommande.tiers_societe = new tiers_societe();
            this.SelectedCommande.totalTTC = SelectedCommande.GetTotalTTC();
            this.SelectedCommande.totalHT = SelectedCommande.GetTotalHT();
            this.LabelboutonSupprimerLigneCommande = "Supprimer la ligne de commande";
            this.EnableBoutonSupprimerLigneCommande = false;

            using (Entity.liv3Entities1 dc = new Entity.liv3Entities1())
            {
                ListClientComplet = dc.tiers_contact.ToList();
                ListClientView = ListClientComplet;
                ListSociete = dc.tiers_societe.ToList();
                ListLignesCommandes = dc.lignecommandeclient.Where(lcc => lcc.id_commandeclient == SelectedCommande.id).ToList();
            }

            SelectedCommande.tiers_contact = ListClientView.FirstOrDefault();
            SelectedCommande.id_tiers_contact = SelectedCommande.tiers_contact.id;

            SelectedSociete = ListSociete.FirstOrDefault();
            SelectedCommande.id_tiers_societe = SelectedCommande.tiers_societe.id;
            if (Etat == "Ajout")
                {
                    SelectedCommande.id = getNewIdCommande();
                    SelectedCommande.synchro = "N";
                    SelectedCommande.dateCreation = DateTime.Now.ToShortDateString()+" "+DateTime.Now.ToShortTimeString();
                    this.LabelboutonValider = "Ajouter";
                    this.LabelboutonRetour = "Annuler";
                    this.EnableCombobox = true;
                    Titre = "CommandeEditView en état: " + Etat + " avec la commande n°" + SelectedCommande.id;
                }
             else if (Etat == "Modification")
                {
                    this.LabelboutonValider = "Valider";
                    this.LabelboutonRetour = "Retour";
                    this.EnableCombobox = false;
                    Titre = "CommandeEditView en état: " + Etat + " avec la commande n°" + SelectedCommande.id;          
                }
            else if (Etat == "AjoutDepuisCreationCommande")
                {
                    this.LabelboutonValider = "Ajouter";
                    this.LabelboutonRetour = "Annuler";
                    this.EnableCombobox = true;
                    Titre = "CommandeEditView en état: " + Etat + " avec la commande n°" + SelectedCommande.id;
                    foreach (lignecommandeclient lcc in this.SelectedCommande.ListNewLigneCommande)
                    {
                        ListLignesCommandes.Add(lcc);
                    }
                }
       
        }

        #endregion

        #region Declaration

        private string _etat;
        public string Etat
        {
            get { return _etat; }
            set
            {
                if (_etat != value)
                {
                    _etat = value;
                    OnPropertyChanged("Etat");
                }
            }
        }

        private commandeclient _selectedCommande;
        public commandeclient SelectedCommande
        {
            get { return _selectedCommande; }
            set
            {
                if (_selectedCommande != value)
                {
                    _selectedCommande = value;

                    OnPropertyChanged("SelectedCommande");
                }
            }
        }
      
        private List<tiers_contact> _listClientComplet;
        public List<tiers_contact> ListClientComplet
        {
            get { return _listClientComplet; }
            set
            {
                if (_listClientComplet != value)
                {
                    _listClientComplet = value;
                    OnPropertyChanged("ListClientComplet");
                }
            }
        }
        private List<tiers_contact> _listClientView;
        public List<tiers_contact> ListClientView
        {
            get { return _listClientView; }
            set
            {
                if (_listClientView != value)
                {
                    _listClientView = value;
                    OnPropertyChanged("ListClientView");
                }
            }
        }

        private List<tiers_societe> _listSociete;
        public List<tiers_societe> ListSociete
        {
            get { return _listSociete; }
            set
            {
                if (_listSociete != value)
                {
                    _listSociete = value;
                    OnPropertyChanged("ListSociete");
                }
            }
        }

        private tiers_societe _selectedSociete;
        public tiers_societe SelectedSociete
        {
            get { return _selectedSociete; }
            set
            {
                if (_selectedSociete != value)
                {
                    _selectedSociete = value;
                    SelectedCommande.tiers_societe = SelectedSociete;
                    LoadComboboxClient();
                    OnPropertyChanged("SelectedSociete");
                }
            }
        }


        private void LoadComboboxClient()
        {
            if (Etat == "Modification")
            {
                SelectedCommande.tiers_contact = ListClientView.Where(tc => tc.id == SelectedCommande.id_tiers_contact).FirstOrDefault();
            }
            else if ((Etat == "Ajout") || (Etat == "AjoutDepuisCreationCommande"))
            {
                using (Entity.liv3Entities1 dc = new Entity.liv3Entities1())
                {
                    ListClientView = ListClientComplet.Where(tc => tc.id_tiers_societe == SelectedSociete.id).ToList();

                    if (ListClientView.Any())
                    {
                        SelectedCommande.tiers_contact = ListClientView.FirstOrDefault();
                        OnPropertyChanged("SelectedCommande");
                    }
                }
            }
        }

        private bool _enableCombobox;
        public bool EnableCombobox
        {
            get { return _enableCombobox; }
            set
            {
                if (_enableCombobox != value)
                {
                    _enableCombobox = value;
                    OnPropertyChanged("EnableCombobox");
                }
            }
        }
         private bool _enableBoutonSupprimerLigneCommande;
        public bool EnableBoutonSupprimerLigneCommande
        {
            get { return _enableBoutonSupprimerLigneCommande; }
            set
            {
                if (_enableBoutonSupprimerLigneCommande != value)
                {
                    _enableBoutonSupprimerLigneCommande = value;
                    OnPropertyChanged("EnableBoutonSupprimerLigneCommande");
                }
            }
        }

        private string _titre;
        public string Titre
        {
            get { return _titre; }
            set
            {
                if (_titre != value)
                {
                    _titre = value;
                    OnPropertyChanged("Titre");
                }
            }
        }

        private string _labelboutonValider;
        public string LabelboutonValider
        {
            get { return _labelboutonValider; }
            set
            {
                if (_labelboutonValider != value)
                {
                    _labelboutonValider = value;
                    OnPropertyChanged("LabelboutonValider");
                }
            }
        }
        private string _labelboutonRetour;
        public string LabelboutonRetour
        {
            get { return _labelboutonRetour; }
            set
            {
                if (_labelboutonRetour != value)
                {
                    _labelboutonRetour = value;
                    OnPropertyChanged("LabelboutonRetour");
                }
            }
        }

        private string _labelInformation;
        public string LabelInformation
        {
            get { return _labelInformation; }
            set
            {
                if (_labelInformation != value)
                {
                    _labelInformation = value;
                    OnPropertyChanged("LabelInformation");
                }
            }
        }
        
        private string _labelboutonSupprimerLigneCommande;
        public string LabelboutonSupprimerLigneCommande
        {
            get { return _labelboutonSupprimerLigneCommande; }
            set
            {
                if (_labelboutonSupprimerLigneCommande != value)
                {
                    _labelboutonSupprimerLigneCommande = value;
                    OnPropertyChanged("LabelboutonSupprimerLigneCommande");
                }
            }
        }

        public List<lignecommandeclient> _listLignesCommandes;
        public List<lignecommandeclient> ListLignesCommandes
        {
            get { return _listLignesCommandes; }
            set
            {
                if (_listLignesCommandes != value)
                {
                    _listLignesCommandes = value;
                    OnPropertyChanged("ListLignesCommandes");
                }
            }
        }
        public lignecommandeclient _selectedLigneCommande;
        public lignecommandeclient SelectedLigneCommande
        {
            get { return _selectedLigneCommande; }
            set
            {
                if (_selectedLigneCommande != value)
                {
                    _selectedLigneCommande = value;
                    this.EnableBoutonSupprimerLigneCommande = true;
                    if ("D".Equals(value.synchro))
                    {
                        this.LabelboutonSupprimerLigneCommande = "Annuler la suppression";
                    }
                    else if ("N".Equals(value.synchro))
                    {
                        this.EnableBoutonSupprimerLigneCommande = false;
                    }
                    else if ("N".Equals(SelectedCommande.synchro))
                    {
                        this.EnableBoutonSupprimerLigneCommande = false;
                        this.LabelInformation = "*Impossible de supprimer une ligne de commande appartenant à une commande non synchronisé.";
                    }
                    else
                    {
                        this.LabelboutonSupprimerLigneCommande = "Supprimer la ligne de commande";
                    }
                    OnPropertyChanged("SelectedLigneCommande");
                }
            }
        }


        #endregion

        #region Methodes

        private void SuppLigneCommande()
        {
            //supprimer la selected commande
            using (Entity.liv3Entities1 dc = new Entity.liv3Entities1())
            {
                if (SelectedLigneCommande != null)
                {
                    if ((SelectedLigneCommande.synchro == "") || (string.IsNullOrEmpty(SelectedLigneCommande.synchro)) || (SelectedLigneCommande.synchro == "N"))
                    {
                        dc.lignecommandeclient.Where(cc => cc.id == SelectedLigneCommande.id).FirstOrDefault().synchro = "D";
                    }
                    else if (SelectedLigneCommande.synchro == "D")
                    {
                        dc.lignecommandeclient.Where(cc => cc.id == SelectedLigneCommande.id).FirstOrDefault().synchro = "";
                    }
                    else if (SelectedLigneCommande.synchro == "N")
                    {
                        MessageBox.Show("nouvelle commande non supprimable");
                    }
                    dc.SaveChanges();
                }
            }

            // rafraichir EditCommandeCLient
            App.Apv.LoadEditCommandeClientView("Modification", SelectedCommande);
  
        }

        public long getNewIdCommande()
        {
            long newID = 0;
            try
            {
                using (Entity.liv3Entities1 dc = new Entity.liv3Entities1())
                {
                    commandeclient LastCommande = new commandeclient();
                    LastCommande = dc.commandeclient.OrderByDescending(cc => cc.id).FirstOrDefault();
                    newID = LastCommande.id + 1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return newID;
        }

        private void Valider()
        {
            try
            {
                //modifier la selected commande   
                if (Etat == "Modification")
                {
                }
                else if (Etat == "Ajout")
                {
                    //ajout du selected commande
                    using (Entity.liv3Entities1 dc = new Entity.liv3Entities1())
                    {
                        Entity.commandeclient myCommandeclient = new Entity.commandeclient { id = getNewIdCommande(), dateCreation = SelectedCommande.dateCreation, statut = SelectedCommande.statut, totalHT = SelectedCommande.GetTotalHT(), totalTTC = SelectedCommande.GetTotalTTC(), montantTva = SelectedCommande.montantTva, id_tiers_societe = SelectedCommande.tiers_societe.id, id_tiers_contact = SelectedCommande.tiers_contact.id, synchro = SelectedCommande.synchro };
                        dc.commandeclient.Add(myCommandeclient);
                        dc.SaveChanges();
                        MessageBox.Show("Commande " + myCommandeclient.id + " ajoutée !");
                    }
                }
                else if (Etat == "AjoutDepuisCreationCommande")
                {

                    //ajout du selected commande
                    using (Entity.liv3Entities1 dc = new Entity.liv3Entities1())
                    {
                        Entity.commandeclient myCommandeclient = new Entity.commandeclient { id = getNewIdCommande(), dateCreation = SelectedCommande.dateCreation, statut = SelectedCommande.statut, totalHT = SelectedCommande.GetTotalHT(), totalTTC = SelectedCommande.GetTotalTTC(), montantTva = SelectedCommande.montantTva, id_tiers_societe = SelectedCommande.id_tiers_societe, id_tiers_contact = SelectedCommande.id_tiers_contact, synchro = SelectedCommande.synchro };
                        dc.commandeclient.Add(myCommandeclient);
                        dc.SaveChanges();
                        MessageBox.Show("Commande " + myCommandeclient.id + " ajoutée !");
                    }
                    //ajout des "newligne commande" 
                    foreach (lignecommandeclient lcc in SelectedCommande.ListNewLigneCommande)
                    {
                        using (Entity.liv3Entities1 dc = new Entity.liv3Entities1())
                        {
                            Entity.lignecommandeclient myLigneCommandeclient = new Entity.lignecommandeclient
                            {
                                id = lcc.id,
                                quantite = lcc.quantite,
                                puHT = lcc.puHT,
                                tauxTva = lcc.tauxTva,
                                totalHT = lcc.totalHT,
                                totalTTC = lcc.totalTTC,
                                commandeclient = dc.commandeclient.Where(cc => cc.id == SelectedCommande.id).FirstOrDefault(),
                                id_commandeclient = SelectedCommande.id,
                                id_produit = lcc.id_produit,
                                synchro = lcc.synchro
                            };
                            dc.lignecommandeclient.Add(myLigneCommandeclient);
                            dc.SaveChanges();
                            MessageBox.Show("Ligne de commande " + lcc.id + " ajoutée !");
                        }
                    }
                }
                Retour();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Retour()
        {
            if (Etat == "Modification")
            {
                using (Entity.liv3Entities1 dc = new Entity.liv3Entities1())
                {
                    dc.commandeclient.Where(cc => cc.id == SelectedCommande.id).FirstOrDefault().totalHT = SelectedCommande.GetTotalHT();
                    dc.commandeclient.Where(cc => cc.id == SelectedCommande.id).FirstOrDefault().totalTTC = SelectedCommande.GetTotalTTC();
                    dc.SaveChanges();
                }
            }
            App.Apv.LoadCommandeClientView();
        }

        public void AjouterLaLigneCommande(){
            if ((Etat == "Ajout") || (Etat == "AjoutDepuisCreationCommande"))
            {
                SelectedCommande.id_tiers_contact = SelectedCommande.tiers_contact.id;
                SelectedCommande.id_tiers_societe = SelectedCommande.tiers_societe.id;
                App.Apv.LoadLigneCommandeEditPage("AjoutDepuisCreationCommande", SelectedCommande);
            }
            else if(Etat == "Modification")
            {
                App.Apv.LoadLigneCommandeEditPage("Ajout", SelectedCommande);
            }
        }

        #endregion

        #region Commandes

        private RelayCommand _ajouterLigneCommand;
        public RelayCommand AjouterLigneCommand
        {
            get
            {
                if (_ajouterLigneCommand == null)
                {
                    _ajouterLigneCommand = new RelayCommand(() => AjouterLaLigneCommande());
                }
                return _ajouterLigneCommand;
            }
        }
        
        private RelayCommand _supprimerLigneCommand;
        public RelayCommand SupprimerLigneCommand
        {
            get
            {
                if (_supprimerLigneCommand == null)
                {
                    _supprimerLigneCommand = new RelayCommand(() => SuppLigneCommande());
                }
                return _supprimerLigneCommand;
            }
        }

        private RelayCommand _boutonOk;
        public RelayCommand BoutonOk
        {
            get
            {
                if (_boutonOk == null)
                {
                    _boutonOk = new RelayCommand(() => Valider());
                }
                return _boutonOk;
            }
        }

        private RelayCommand _boutonRetour;
        public RelayCommand BoutonRetour
        {
            get
            {
                if (_boutonRetour == null)
                {
                    _boutonRetour = new RelayCommand(() => Retour());
                }
                return _boutonRetour;
            }
        }


        #endregion
    }
}
